package zxk.spider.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import zxk.spider.domain.MovieEntity;
import zxk.spider.mapper.MovieMapper;
import zxk.spider.service.MovieService;
import zxk.spider.webmagic.pipline.MyPipeline;
import zxk.spider.webmagic.processor.MoviePageProcessor;

/**
 * @fileName: MovieServiceImpl
 * @author: 舟小亢
 * @date: 2022-11-06 20:05
 * @description:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MovieServiceImpl extends ServiceImpl<MovieMapper, MovieEntity> implements MovieService {

    private final MovieMapper movieMapper;
    private final MyPipeline myPipeline;


    @Override
    public void upYear(MovieEntity movieEntity) {
        movieMapper.upYear(movieEntity);
    }

    /** 爬虫初始网站 */
    private static final String url = "https://www.meijutt.tv/1_______.html";

//    @Scheduled(fixedDelay = 100000000 * 1000)
    @Override
    public void spiderMovie() {
        System.out.println("我是分支");
        System.out.println("我是主线");
        Spider.create(new MoviePageProcessor())
                //开始抓取的位置
                .addUrl(url)
                //开启队列加去重（最大支持10万条数据）
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(100000)))
                //持久化
                .addPipeline(myPipeline)
                //开启线程
                .thread(40)
                //启动爬虫
                .run();
    }

}
